﻿
layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
    $ = layui.jquery;


    //登录按钮
    form.on("submit(login)", function (data) {
        $(this).text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");
        var loginForm = document.forms["loginForm"];
        var phone = loginForm.elements["phone"].value;
        var password = loginForm.elements["password"].value;
      //  var verifyCode = loginForm.elements["verifyCode"] ? loginForm.elements["verifyCode"].value : undefined;
       // var identity = loginForm.elements["identity"].value;

        var parms = new Object();
        parms["phone"] = phone;
        parms["password"] = password;
       // parms["verifyCode"] = verifyCode;
        //parms["identity"] = identity;
        $.post("/Login/LoginIn", parms, loginResponse)
     
        return false;
    })

    //处理登录的反馈信息
    function loginResponse(data) {
        var result = data;
        console.log(result)
        if (result.status == 1) {
            window.location.href = "/Home/Index";
            $(".btn-submit").html("登录成功，跳转中...");
            //if (result.data == 0) {
              
            //}
            //else {
            //    window.location.href = "/MiniProgram/Home/Index";
            //    $(".btn-submit").html("登录成功，跳转中...");
            //}
        }
        else {
            if (result.data == -2) {//密码不符合要求
                var phone = loginForm.elements["Login"].value;
               // var identity = loginForm.elements["identity"].value;
                layer.open({
                    type: 2,
                    title: '密码不符合要求，请修改密码',
                    area: ['350px', '390px'],
                    content: "/Login/ForgetPwd?phone=" + phone + "&identity=" + identity,
                    success: function (layero, index) {
                    },
                    yes: function (index, layero) {
                        layero.find("iframe").contents().find("#btn_submit").click();
                    },
                    cancel: function (index, layero) {
                        $(".btn-submit").text("登录").attr('disabled', false).removeClass('layui-disabled');
                    },
                    end: function (index, layero) {
                        $(".btn-submit").text("登录").attr('disabled', false).removeClass('layui-disabled');
                    }
                });
            }
            else if (result.data == -1) {//需要使用验证码登录
                var phone = loginForm.elements["Login"].value;
                var identity = loginForm.elements["identity"].value;
                layer.open({
                    type: 2,
                    title: '短信验证码登录',
                    area: ['350px', '230px'],
                    content: "/Login/SmsCodeLogin?phone=" + phone + "&identity=" + identity,
                    success: function (layero, index) {
                    },
                    yes: function (index, layero) {
                        layero.find("iframe").contents().find("#btn_submit").click();
                    },
                    cancel: function (index, layero) {
                        $(".btn-submit").text("登录").attr('disabled', false).removeClass('layui-disabled');
                    },
                    end: function (index, layero) {
                        $(".btn-submit").text("登录").attr('disabled', false).removeClass('layui-disabled');
                    }
                });
                /*$.layerMsg(result.message, result.state);*/
            }
            else {
                $(".btn-submit").html("立即登录");
                $(".btn-submit").attr('disabled', false).removeClass('layui-disabled');
                //$("#verifyCode").trigger('click');//重新生成验证码
                $.layerMsg(result.message, result.state);
                GetYzcode();
            }
        }
    }

    $(".layui-form-item .layui-input").focus(function () {
        $(this).parent().addClass("layui-input-focus");
    })
    $(".layui-form-item .layui-input").blur(function () {
        $(this).parent().removeClass("layui-input-focus");
        if ($(this).val() != '') {
            $(this).parent().addClass("layui-input-active");
        } else {
            $(this).parent().removeClass("layui-input-active");
        }
    })
})